﻿Public Class TabContorlKai
    Inherits TabControl

    Public Event HScroll As ScrollEventHandler
    Private iOldVal As Integer = 0
    Private iNewVal As Integer = 0
    Dim scrllbar As System.Windows.Forms.HScrollBar

    Private iPos As Integer = 0

    Protected Sub OnScroll(ByVal sender As Object, ByVal e As ScrollEventArgs) 'Handles Me.HScroll
        RaiseEvent HScroll(sender, e)

        If e.Type = ScrollEventType.EndScroll Then
            iOldVal = e.NewValue
            'Debug.Print(String.Format("{0}-->{1}", e.OldValue, e.NewValue))
        End If
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)
        If m.Msg = &H114 Then
            Dim scrollType As ScrollEventType
            scrollType = CType(LoWord(m.WParam), ScrollEventType)
            OnScroll(Me, New ScrollEventArgs(scrollType, 200))
            'Debug.Print(m.ToString)

        End If
    End Sub

    Private Function LoWord(ByVal dWord As IntPtr) As Integer
        Return dWord.ToInt32 And Convert.ToInt32(&HFFFF)
    End Function

    Private Function HiWord(ByVal dWord As IntPtr) As Integer
        If (dWord.ToInt32 And &H80000000) = &H80000000 Then
            Return dWord.ToInt32 >> 16
        Else
            Return (dWord.ToInt32 >> 16) And &HFFFF
        End If
    End Function

    Public Sub LeftScroll()
        Dim m As Message
        iPos -= 1
        If iPos < 0 Then
            iPos = 0
        End If
        m.Msg = &H114
        'm.HWnd = OnScroll.
        m.WParam = &H4 + (iPos << 16)
        WndProc(m)
        m.WParam = &H8 + (iPos << 16)
        WndProc(m)
    End Sub

    Public Sub RightScroll()
        Dim m As Message
        iPos += 1
        If iPos > Me.TabCount Then
            iPos = Me.TabCount
        End If
        m.Msg = &H114
        'm.HWnd = CInt(AddressOf OnScroll)
        m.WParam = &H4 + (iPos << 16)
        WndProc(m)
        m.WParam = &H8 + (iPos << 16)
        WndProc(m)
    End Sub

End Class
